package emergency;

import java.util.ArrayList;

import movement.Location;

import unit.Unit;

public class EmergencyPolicyDefault extends EmergencyPolicy {

	/**
	 * Method for sorting units, depending on which concrete emergency policy is
	 * used
	 * 
	 * @param units
	 *            , all the units
	 * 
	 * @param e
	 *            , the emergency you want to base the sorting on ( the location
	 *            is important )
	 * 
	 * @return Returns the list with units, sorted on distance to the emergency
	 */
	@Override
	public ArrayList<Unit> sortUnits(ArrayList<Unit> units, Emergency e) {
		Location targetLocation = e.getLocation();
		ArrayList<Unit> result = new ArrayList<Unit>();
		Unit closest = null;

		while (units.size() > 0) {
			double closestDistance = Double.MAX_VALUE;
			for (Unit u : units) {
				double afstand = targetLocation
						.distance(u.getCurrentLocation());
				if (afstand < closestDistance) {
					closest = u;
					closestDistance = afstand;
				}
			}
			result.add(closest);
			units.remove(closest);
		}
		return result;
	}

	/**
	 * Method for getting a string representation of this emergency policy
	 * 
	 * @return a string representation of this emergency policy
	 */
	@Override
	public String toString() {
		return "Default Emergencypolicy";
	}
}
